package Xmlcompare;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
 
/**
 * Contains some methods to list files and folders from a directory
 *
 * @author Loiane Groner
 * http://loiane.com (Portuguese)
 * http://loianegroner.com (English)
 */

public class ListFilesUtil {
 
    /**
     * List all the files and folders from a directory
     * @param directoryName to be listed
     */
	public PrintWriter  log=BXmlCompare.log;
    public void listFilesAndFolders(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            System.out.println(file.getName());
        }
    }
 
    /**
     * List all the files under a directory
     * @param directoryName to be listed
     * @throws IOException 
     */
    public void listFiles(String directoryName) throws IOException  {
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
        String a1 [] = null;
        for (File file : fList){
            if (file.isFile()){
            	if(file.getName().substring(file.getName().lastIndexOf(".")+1,file.getName().length()).equalsIgnoreCase("xml"))
            	{
            		//System.out.println(generator.Dest1+File.separator+file.getName());
            		System.out.println("Verifying "+file.getName());
            		
            		XMLValueComp.Comparexml(BXmlCompare.Dest1+File.separator+file.getName(),BXmlCompare.Dest2+File.separator+file.getName());
            	}
            	else
            	{
            		System.out.println("Internal Error"+directoryName+File.separator+file.getName());
            	}
            	new File(BXmlCompare.Dest1+File.separator+file.getName()).delete();
            	new File(BXmlCompare.Dest2+File.separator+file.getName()).delete();
            	/*
            	a1[0]=directoryName+File.separator+file.getName();
            	a1[1]=directoryName+File.separator+"Result"+file.getName();
            	a1[2]=directoryName+File.separator+"Log"+file.getName();*/
            	
                //count.main1(directoryName+File.separator+file.getName());
            	// System.out.println(directoryName+File.separator+file.getName());
            }
           
            
        }
        listremainingFiles(BXmlCompare.Dest1,0);
        listremainingFiles(BXmlCompare.Dest2,1);
    }
    
    
    
    public void listremainingFiles(String directoryName,int flag){
    	 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
        System.out.println(fList.length);  
 if(fList.length>0)
 {
	 if(flag==0)
	 {
		 log.println("Etl Output Contains the Following files which doesn't Exist  in expected Output");
		 for (File file : fList){
	         if (file.isFile()){
	             System.out.println(file.getName());
	             log.println(file.getName());
	             //file.delete();
	         }
	     }
		 //directory.delete();
	 }
	 if (flag==1)
	 {
		 log.println("Following files are not  get generated from Etl Batch File");
		 for (File file : fList){
	         if (file.isFile()){
	             System.out.println(file.getName());
	             log.println(file.getName());
	             //file.delete();
	         }
	     }
		
	 }	
 }
 else
 {
	 directory.delete();
 }
       
    }
 
    /**
     * List all the folder under a directory
     * @param directoryName to be listed
     */
    public void listFolders(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isDirectory()){
                System.out.println(file.getName());
            }
        }
    }
 
    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     */
    public void listFilesAndFilesSubDirectories(String directoryName){
 
        File directory = new File(directoryName);
 
        //get all the files from a directory
        File[] fList = directory.listFiles();
 
        for (File file : fList){
            if (file.isFile()){
                System.out.println(file.getAbsolutePath());
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }
 public static String dest=null,tc=null;
    public static void main (String[] args) throws IOException{
 
        ListFilesUtil listFilesUtil = new ListFilesUtil();
 
        final String directoryLinuxMac ="/Users/loiane/test";
 
        //Windows directory example
        final String directoryWindows =args[0];
 System.out.println(directoryWindows);
 String directoryWindows1=directoryWindows.substring(0, directoryWindows.lastIndexOf(File.separator));
 dest=directoryWindows1+File.separator+"out";
 tc=directoryWindows1+File.separator+"tc"+Math.round((Math.random()*10000));
 if(new File(tc).exists())
 {
	 new File(tc).delete();
	 
 }
 new File(dest).mkdirs();
 new File(tc).mkdirs();
        listFilesUtil.listFiles(directoryWindows);
    }
}